<template>
	<view class="main-box">
		<view class="status-bar" :style="{ height: iStatusBarHeight + 'px'}"></view>
		<view ref="topBar" class="box-header" style="position: fixed;top:0;left: 0;width: 100%;z-index: 100;">
			<view class="top-bar" :style="{ 'padding-top': iStatusBarHeight + 'px'}">
				<view class="search-box">
					<u-search placeholder="搜一搜" v-model="searchWord" bg-color="#ffffff" :disabled="true" search-icon="" height="68"
					 :show-action="false" @click="handleToSearch"></u-search>
					<!-- <u-icon @click="handleToLibrary" name="grid-fill" color="#ffffff" size="32" :custom-style="{ background:'#061B41',width:'60rpx',height:'60rpx','border-radius':'30rpx','display':'inline-flex','justify-content':'center','align-items':'center','margin-left':'20rpx'}"></u-icon> -->
				</view>
				<view>
					<u-tabs-swiper ref="uTabs" :list="typeList" :current="tabCurrent" @change="tabsChange" swiperWidth="750" bg-color="#f5f8fc"
					 bar-height="6" :bar-width="16" active-color="#92c8ff" inactive-color="#061B41" font-size="32" :is-scroll="false"></u-tabs-swiper>
				</view>
			</view>
		</view>

		<view class="swiper-wrap" :style="{height:screenHeight - 140 + 'px'}">
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="(item, index) in typeList" :key="index">
					<IndexItem :bookType="typeList[index]" ref="indexItem" />
				</swiper-item>
			</swiper>
		</view>
		<view class="refresh" v-show="errorFlag">
			<u-button :ripple="true" style="margin-left: 0;" @click="getBookType">重新加载</u-button>
		</view>
		<u-tabbar ref="tabBar" v-model="currentTabbar" :list="vuex_tabbar"></u-tabbar>
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	import {
		getUpdateList,
		getBannerList
	} from '../../api/homepage.js'
	import {
		getSearchList
	} from '../../api/list.js'
	import {
		getBookType
	} from '../../api/common.js'
	import IndexItem from '../../components/IndexItem'
	export default {
		components: {
			IndexItem
		},
		data() {
			return {
				searchWord: '',
				typeList: [],
				tabCurrent: 0,
				swiperCurrent: 0,
				currentTabbar: 0,
				swiperHeight: 0,
				pageList: [],
				scrollHeight: 40,
				netWorkFlag: null,
				errorFlag:false
			}
		},
		computed: {
			bookType() {
				for (const item of this.typeList) {
					if (item.id == this.swiperCurrent + 1) {
						return item.name;
					}
				}
			},
		},
		onLoad(options) {
			console.log(options)
			this.init();
		},
		updated() {
			this.pageList = this.$refs.indexItem;
			this.switchTab(this.tabCurrent);
		},
		methods: {
			init(){
				uni.getNetworkType({
					success: (res) => {
						console.log(res.networkType);
						if (res.networkType == 'none') {
							this.netWorkFlag = setInterval(() => {
								uni.onNetworkStatusChange((res) => {
									if (res.isConnected) {
										this.getBookType();
										clearInterval(this.netWorkFlag);
									}
								});
							}, 1000)
						} else {
							this.getBookType();
						}
					}
				});
			},
			switchTab(index) {
				if (this.pageList[index].bannerList.length === 0) {
					this.pageList[index].getData();
				}
				if (this.tabCurrent === index) {
					return;
				}
			},
			// 获取分类
			getBookType() {
				return new Promise((resolve, reject) => {
					getBookType().then((res) => {
						res.data.bookType.forEach((item, index) => {
							res.data.bookType[index].name = item.name.slice(0, 2);
						});
						this.typeList = res.data.bookType;
						this.errorFlag = false;
						resolve('ok')
					}).catch(error => {
						this.errorFlag = true;
						reject('error')
					});
				})
			},
			// tabs通知swiper切换
			async tabsChange(index) {
				this.swiperCurrent = index;
			},
			// swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.tabCurrent = current;
				this.switchTab(this.tabCurrent);
			},
			handleToLibrary() {
				uni.switchTab({
					url: `/pages/library/library`
				});
			},
			handleToSearch() {
				uni.navigateTo({
					url: `/pages/search/search`
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main-box {
		.status-bar {
			height: var(--status-bar-height);
			width: 100%;
			background: #F5F8FC;
		}

		.box-header {
			border-radius: 0 0 40rpx 40rpx;
			background: #F5F8FC;

			.box-title {
				display: flex;
				justify-content: space-between;
				padding: 32rpx 32rpx 0rpx 32rpx;
				margin-bottom: 10rpx;
				font-family: Alibaba PuHuiTi;

				text {
					font-size: 40rpx;
					font-weight: 800;
					color: rgba(6, 27, 65, 0.75);
				}
			}

			.top-bar {
				padding: 40rpx 32rpx 20rpx 32rpx;
				background: #F5F8FC;

				.search-box {
					display: flex;
					margin-top: 20rpx;
				}
			}

		}

		.swiper-wrap {
			// margin-top: 168rpx;
			display: flex;
			flex-direction: column;
			height: calc(100vh - var(--window-top));
			width: 100%;
			position: relative;
			top: 168rpx;

			.swiper-box {
				flex: 1;

				.swiper-item {
					flex: 1;
					flex-direction: column;
				}
			}
		}
		
		.refresh{
			position: fixed;
			top: 400rpx;
			left: 0;
			height: 300rpx;
			width: 60vw;
			margin: 0 20vw;
			}
	}
</style>
